package com.sunwayworld.basemodule.coredata.productreport.service.impl;

import com.sunwayworld.basemodule.coredata.product.bean.LimsProductMainBean;
import com.sunwayworld.basemodule.coredata.product.service.LimsProductMainService;
import com.sunwayworld.framework.exception.InvalidDataException;
import com.sunwayworld.framework.mybatis.mapper.MapperParameter;
import com.sunwayworld.framework.mybatis.mapper.MatchPattern;
import com.sunwayworld.framework.mybatis.mapper.SearchFilter;
import com.sunwayworld.framework.utils.CollectionUtils;
import com.sunwayworld.framework.utils.NumberUtils;
import com.sunwayworld.framework.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.sunwayworld.framework.at.annotation.AuditTrailType;
import com.sunwayworld.framework.at.annotation.AuditTrailEntry;
import com.sunwayworld.framework.spring.annotation.GikamBean;

import com.sunwayworld.basemodule.coredata.productreport.dao.LimsReportClientProductDao;
import com.sunwayworld.basemodule.coredata.productreport.bean.LimsReportClientProductBean;
import com.sunwayworld.basemodule.coredata.productreport.service.LimsReportClientProductService;
import com.sunwayworld.framework.context.ApplicationContextHelper;
import com.sunwayworld.framework.restful.data.RestJsonWrapperBean;

import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

@Repository
@GikamBean
public class LimsReportClientProductServiceImpl implements LimsReportClientProductService {

    @Autowired
    private LimsReportClientProductDao limsReportClientProductDao;

    @Lazy
    @Autowired
    private LimsProductMainService productMainService;

    @Override
    @SuppressWarnings("unchecked")
    public LimsReportClientProductDao getDao() {
        return limsReportClientProductDao;
    }

    @Override
    @Transactional
    @AuditTrailEntry(AuditTrailType.INSERT)
    public Long insert(RestJsonWrapperBean jsonWrapper) {
        LimsReportClientProductBean clientProductGroupBean = jsonWrapper.parseUnique(LimsReportClientProductBean.class);
        String productIds = clientProductGroupBean.getExt$Item("productIds");
        List<Long> productIdList = Arrays.stream(productIds.split(",")).map(NumberUtils::parseLong).collect(Collectors.toList());
        List<LimsProductMainBean> productGroupBeans = productMainService.getDao().selectListByIds(productIdList);
        List<Long> idList = ApplicationContextHelper.getNextIdentityList(productIdList.size());
        List<LimsReportClientProductBean> clientProductGroupBeans = productGroupBeans.stream().map(entity -> {
            LimsReportClientProductBean bean = new LimsReportClientProductBean();
            bean.setId(idList.remove(0));
            bean.setClientId(clientProductGroupBean.getClientId());
            bean.setClientCode(clientProductGroupBean.getClientCode());
            bean.setClientName(clientProductGroupBean.getClientName());
            bean.setReportTemplateId(clientProductGroupBean.getReportTemplateId());
            bean.setProductId(entity.getId());
            bean.setProductCode(entity.getProductCode());
            return bean;
        }).collect(Collectors.toList());
        MapperParameter temp = new MapperParameter();
        temp.setFilter(SearchFilter.instance().match("clientCode",clientProductGroupBean.getClientCode()).filter(MatchPattern.EQ));
        temp.setFilter(SearchFilter.instance().match("reportTemplateId",clientProductGroupBeans.stream().map(LimsReportClientProductBean::getReportTemplateId).collect(Collectors.toList())).filter(MatchPattern.OR));
        List<LimsReportClientProductBean> existsBeanList = selectList(temp);
        if (!CollectionUtils.isEmpty(existsBeanList)){
            Optional<LimsReportClientProductBean> any = existsBeanList.stream().filter(b -> productIdList.contains(b.getProductId())).findAny();
            if (any.isPresent()){
                List<LimsReportClientProductBean> groupBeans = existsBeanList.stream().filter(f -> productIdList.contains(f.getProductId())).distinct().collect(Collectors.toList());
                StringBuffer sb = new StringBuffer();
                sb.append("以下检测组合：<br>");
                String msg = StringUtils.join(groupBeans.stream().map(LimsReportClientProductBean::getProductCode).collect(Collectors.toList()), "<br>");
                sb.append(msg+"<br>");
                sb.append("不能重复维护！");
                throw new InvalidDataException(sb.toString());
            }
        }
        getDao().insert(clientProductGroupBeans);
        return clientProductGroupBeans.get(0).getId();
    }

}
